<?

/**
 * DevSuite
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://suite.rapidcodefactory.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@rapidcodefactory.com so we can send you a copy immediately.
 *
 * @category   RFC_Model
 * @package    RCF_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
 * @version    
*/



/**
 * Class for DB_Object Model
 *
 * @category   RFC_Model
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class Ratings extends DomainModel {
	protected $id;	//primaryKey=yes, dataType=int, maxLength=, allowNulls=no
	protected $contentTypesId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=no
	protected $objectId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=yes
	protected $usersId;	//primaryKey=no, dataType=int, maxLength=, allowNulls=yes
	protected $addDate;	//primaryKey=no, dataType=datetime, maxLength=, allowNulls=no
	protected $rating;	//primaryKey=no, dataType=smallint, maxLength=, allowNulls=yes
	protected $xmlData;	//primaryKey=no, dataType=varchar, maxLength=500, allowNulls=yes
}




/**
 * Class for DB_Object ModelMapper
 *
 * @category   RFC_Model
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class RatingsMapper extends ModelMapper {
	function __construct(){
		parent::__construct();
		$this->tableName = 'ratings';
		$this->selectViewName = 'v_ratings';
		$this->selectFields = '
				id,
				content_types_id,
				object_id,
				users_id,
				add_date,
				rating,
				xml_data,
				ct_id,
			  	ct_type,
			  	u_id,
			    u_apps_id,
			    u_statuses_id,
			    u_username,
			    u_first_name,
			    u_last_name,
			    u_email,
			    u_is_operator,
			    u_is_administrator,
			    u_remarks
		';

		$this->findQuery = self::$DB->prepare("
			SELECT ".$this->selectFields."
			FROM ".$this->selectViewName."
			WHERE id = ?
		");
		$this->insertQuery = self::$DB->prepare("
			CALL p_add_ratings (@p_id,
				?,
				?,
				?,
				?,
				?,
				?
		)");


		$this->updateQuery = self::$DB->prepare("
			CALL p_upd_ratings (
				?,
				?,
				?,
				?,
				?,
				?,
				?

		)");

		$this->deleteQuery = self::$DB->prepare("
			CALL p_del_ratings (?)
		");

		$this->cacheTime = null;
	}

	protected function doLoad($array){
		$acuire = str_replace('Mapper', '', get_class($this));
		$obj = new $acuire($array['id']);
		$obj->setId($array['id']);
		$obj->setContentTypesId($array['content_types_id']);
		$obj->setObjectId($array['object_id']);
		$obj->setUsersId($array['users_id']);
		$obj->setAddDate($array['add_date']);
		$obj->setRating($array['rating']);
		$obj->setXmlData($array['xml_data']);

		return $obj;
	}

	function insert(DomainModel $obj){

		$values = array(
			$obj->getContentTypesId(),
			$obj->getObjectId(),
			$obj->getUsersId(),
			$obj->getAddDate(),
			$obj->getRating(),
			$obj->getXmlData()
		);

		$this->doStatement($this->insertQuery, $values);
		$obj->setId(self::$DB->getOne("SELECT @p_id"));
	}

	function update(DomainModel $obj){
		$values = array(
			$obj->getId(),
			$obj->getContentTypesId(),
			$obj->getObjectId(),
			$obj->getUsersId(),
			$obj->getAddDate(),
			$obj->getRating(),
			$obj->getXmlData()
		);

		$this->doStatement($this->updateQuery, $values);
	}

	function delete(DomainModel $obj){
		$this->doStatement($this->deleteQuery, array($obj->getId()));
	}

	function newId(){

		$row = self::$DB->getRow('SHOW TABLE STATUS LIKE ratings');
		return $row['Auto_increment'];

	}
}


/**
 * Class for DB_Object Model
 *
 * @category   RFC_ModelCollection
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class RatingsCollection extends ModelCollection {
	function add(Ratings $model){
		$this->doAdd($model);
	}
}


/**
 * Class for DB_Object Model
 *
 * @category   RFC_ModelCollectionExtended
 * @package    RFC_DModel
 * @copyright  Copyright (c) 2010-2011 Rapid Code Factory Ltd. POLAND (http://www.rapidcodefactory.com)
 * @license    http://suite.rapidcodefactory.com/license/new-bsd     New BSD License
*/
class RatingsCollectionExtended extends RatingsCollection {
	private $stmt;
	private $valueArray;
	private $mapper;
	private $run = false;

	function __construct(ModelMapper $mapper, $stmtHandle, $valueArray){
		parent::__construct();
		$this->stmt = $stmtHandle;
		$this->valueArray = $valueArray;
		$this->mapper = $mapper;
	}

	function notifyAccess(){
		if (!$this->run){
			$result = $this->mapper->doStatement($this->stmt, $this->valueArray,$this->mapper->getCacheTime());
			$this->init_db($result, $this->mapper);
			$this->run = true;
		}
	}
}

?>
